/**
 * Created by xml on 2019/1/3.
 */
import api from '../../../axios'
import Header from '../../../components/Header.vue';
export default {
	name: "orderInfo",
	components: {
		Header
	},
	mounted: function() {
		this.showGroupList();
		this.getProducts();
		this.getProductPrices();
		this.query(1);
	},
	methods: {

		//查询
		query: function(pageNum) {
			this.loading = true;
			let params = {
				companyId: this.searchForm.companyId === '' ? null : this.searchForm.companyId
			};
			let url = '/api/order/queryForPage?pageIndex=' + pageNum + '&pageSize=' + this.pageInfo.pageSize;
			this.axios.hPost(url, params, (response) => {
				if (!response.success) {
					this.$message.error(response.msg);
				}
				this.itemList = response.data.list;
				this.pageInfo.totalNum = response.data.totalCount;
				this.pageInfo.currentPage = pageNum;

				this.loading = false;
			});
		},

		//显示增加窗口()
		showAddDialog() {
			this.dialogTitle = this.$t('orderInfo.createOrder');
			this.addForm.saving = false;
			this.addFormVisible = true;
			if (this.$refs['addForm']) {
				this.$refs['addForm'].resetFields();
			}
			this.addForm.id = '';
			this.addForm.companyId = '';
			this.addForm.productIds = [];
			this.addForm.faceMonNum = 3;
			this.addForm.imgMonNum = 3;
			this.addForm.meetingMonNum = 3;
		},

		//创建订单
		createOrder() {
			this.addForm.saving = true;
			let url = '/api/order/submit';
			this.$refs['addForm'].validate((valid) => {
				if (valid) {
					let params = {
						id: this.addForm.id,
						companyId: this.addForm.companyId,
						productIds: this.addForm.productIds,
						faceMonNum: this.addForm.productIds.indexOf(1) == -1 ? '' : this.addForm.faceMonNum,
						imgMonNum: this.addForm.productIds.indexOf(2) == -1 ? '' : this.addForm.imgMonNum,
						meetingMonNum: this.addForm.productIds.indexOf(4) == -1 ? '' : this.addForm.meetingMonNum,
					};
					this.axios.hPost(url, params, (response) => {
						if (response.success) {
							this.$message.success(response.msg);
							this.addFormVisible = false;
							this.query(1);
						} else {
							this.$message.error(response.msg);
							this.addForm.saving = false;
						}
					});
				} else {
					this.addForm.saving = false;
					return false;
				}
			});
		},

		//获取产品服务
		getProducts() {
			api.hGet("/api/product/list", "", (data) => {
				if (data.success) {
					this.content = data.data;
				}
			});
			//console.log(this.content);
		},

		//查询所有母公司
		showGroupList() {
			this.axios.hPost("/api/company/querys", {}, (data) => {
				if (data.data) {
					this.companyList = data.data;
				}
				//console.log(this.companyList)
			});
		},

		//查询产品价格信息
		getProductPrices() {
			api.hGet("/api/product/findProductPrice", "", (data) => {
				if (data.success) {
					this.productPriceList = data.data;
				}
			});
			//console.log(this.productPriceList);
		},

		//分页size变化
		handleSizeChange(val) {
			this.pageInfo.pageSize = val;
			this.query(1);
		},
		//翻页
		handleCurrentChange(val) {
			this.query(parseInt(val));
		}
	},
	data() {
		return {
			//是否在加载标记
			loading: false,
			//存放搜索框数据
			searchForm: {
				companyId: '',
				companyName: ''
			},
			addForm: {
				id: '',
				companyId: '',
				companyName: '',
				productIds: [],
				productName: [],
				//productService: [],
				//monNums: [],
				faceMonNum: '',
				imgMonNum: '',
				meetingMonNum: '',
				saving: false
			},
			//存放分页数据
			pageInfo: {
				currentPage: 1,
				pageSize: 10,
				totalNum: 0
			},
			//后台返回的数据
			itemList: [],
			//所有母公司集合
			companyList: [],
			//checkList: [],
			productPriceList: [],
			dialogTitle: '',
			addFormVisible: false,
			content: '',

			addRules: {
				companyId: [{
					required: true,
					message: this.$t('message.notCompany'),
					trigger: 'change'
				}],
				productIds: [{
					required: true,
					message: this.$t('message.notService'),
					trigger: 'change'
				}]
			}

		}
	}
}
